<template>
  <div>
    <SupInput :InpList="InpList" :BtnList="BtnList" @clickFn="btnFn"></SupInput>
    <Suptable
      :headerData="headerData"
      :tableData="tableData"
      :total="total"
      :pagesizeList="fenye"
      @handleSizeChange="handleSizeChange"
      @handleCurrentChange="handleCurrentChange"
      :isTag="true"
      :TagName="TagName"
    >
      <template v-slot:tagSlot="{ row }">
        <el-tag>{{ row.row.status }}</el-tag>
      </template>
      <template v-slot:opera="{ row }">
        <el-button type="primary" @click="editClick(row.row)" size="small" :disabled="row.row.status=='已回复'?true:false"
          >回复</el-button
        >
        <el-button type="danger" @click="delClick(row.row.id)" size="small"
          >删除</el-button
        >
      </template>
    </Suptable>
    <div>
      <el-dialog :visible.sync="dialogVisible" width="30%">
        <div>
          <el-form
            :model="ruleForm"
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item label="评论内容" prop="goodsreview">
              <el-input v-model="ruleForm.goodsreview"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submitForm('ruleForm')"
                >确定</el-button
              >
            </el-form-item>
          </el-form>
        </div>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import {
  reviewList,
  detreview,
  queryreview,
  editreview,
} from "@/api/reviewMock.js";
import Suptable from "@/components/Suptable";
import SupInput from "@/components/SupInput";
export default {
  name: "review",
  data() {
    return {
      ruleForm: {
        goodsreview: "",
      },
      rules: {
        goodsreview: [
          { required: true, message: "请输入商品名称", trigger: "blur" },
        ],
      },
      editId:"",
      dialogVisible: false,
      TagName: "是否回复",
      pagesize: 10,
      pagenum: 1,
      total: 0,
      fenye: [5, 10, 15, 20],
      tableData: [],
      headerData: [
        {
          prop: "id",
          label: "编号",
        },
        {
          prop: "name",
          label: "用户",
        },
        {
          prop: "goodsname",
          label: "评价商品",
        },
        {
          prop: "class",
          label: "打分",
        },
        {
          prop: "goodsreview",
          label: "评论内容",
        },
        {
          prop: "addTime",
          label: "时间",
        },
        {
          prop:'reply',
          label:'回复内容'
        }
      ],
      InpList: [
        {
          model: "",
          text: "请输入用户名称",
        },
        {
          model: "",
          text: "请输入商品序列号",
        },
      ],
      BtnList: [
        {
          icon: "el-icon-search",
          name: "查找",
        },
      ],
    };
  },
  mounted() {
    let _self = this;
    _self.ReviewList();
  },
  methods: {
    submitForm(formName) {
      let _self = this;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let data={
            id: _self.editId,
            reply: _self.ruleForm.goodsreview
          };
           editreview(data).then((res) => {
              // console.log(res);
              if (res.meta.status == 200) {
                _self.ReviewList();
                _self.$message("更改成功");
                _self.dialogVisible = false;
                _self.$refs[formName].resetFields();
              }
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 编辑回复内容
    editClick(row) {
      let _self = this;
      _self.dialogVisible = true;
      _self.ruleForm.goodsreview = row.reply;
     _self.editId = row.id
    },
    // 删除评论
    delClick(id) {
      let _self = this;
      // 用户的删除必须先提示
      _self
        .$confirm("确定删除吗?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
        .then(() => {
          let data = { id };
          detreview(data).then((res) => {
            // console.log(res);
            if (res.meta.status == 200) {
              _self.$message({
                type: "success",
                message: "删除成功!",
              });
              _self.ReviewList();
            } else {
              _self.$message({
                type: "info",
                message: res.meta.msg,
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 获取评论列表
    ReviewList() {
      let _self = this;
      let data = {
        pagesize: _self.pagesize,
        pagenum: _self.pagenum,
      };
      reviewList(data).then((res) => {
        // console.log(res);
        let _self = this;
        _self.tableData = [];
        for (let item of res.data.user) {
          let ReviewData = {
            id: item.id,
            addTime: item.addTime,
            class: item.class,
            goodsname: item.goodsname,
            goodsreview: item.goodsreview,
            name: item.name,
            status: item.status == 0 ? "已回复" : "未回复",
            reply:item.reply
          };
          _self.tableData.push(ReviewData);
        }
        _self.total = res.data.total;
        _self.pagenum = res.data.pagenum;
      });
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      let _self = this;
      _self.pagesize = val;
      _self.ReviewList();
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      let _self = this;
      _self.pagenum = val;
      _self.ReviewList();
    },
    btnFn(index) {
      let _self = this;
      if (index == 0) {
        let data = {
          name: _self.InpList[0].model,
          id: _self.InpList[1].model,
        };
        queryreview(data).then((res) => {
          // console.log(res);
          if (res.meta.status == 200) {
            _self.tableData = [];
            for (let item of res.data.arr) {
              let ReviewData = {
                id: item.id,
                addTime: item.addTime,
                class: item.class,
                goodsname: item.goodsname,
                goodsreview: item.goodsreview,
                name: item.name,
                status: item.status == 0 ? "回复" : "未回复",
              };
              _self.tableData.push(ReviewData);
            }
          } else {
            _self.ReviewList();
          }
        });
      }
    },
  },
  components: {
    Suptable,
    SupInput,
  },
};
</script>